import { createSlice } from "@reduxjs/toolkit";
import { setStorage, getStorage, removeStorage } from "../storage";

const CACHE_MENU_KEY = "app_menu";

export const counterSlice = createSlice({
  name: "app",
  initialState: {
    menus: getStorage(CACHE_MENU_KEY) || [],
  },
  reducers: {
    setAppData: (state, action) => {
      state.menus = action.payload;
      setStorage(CACHE_MENU_KEY, action.payload);
    },
    removeAppData: (state) => {
      state.routes = [];
      state.menus = [];
      removeStorage(CACHE_MENU_KEY);
    },
  },
});

export const { setAppData, removeAppData } = counterSlice.actions;

export default counterSlice.reducer;
